package com.guxingke.typecho4j.widget.handle.content;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.Content;
import com.guxingke.typecho4j.route.DispatcherStrategy;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.widget.Widget;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.layout.LayoutWidget;
import com.guxingke.typecho4j.widget.layout.front.ArchiveLayoutWidget;
import com.guxingke.typecho4j.widget.layout.front.IndexLayoutWidget;
import com.jfinal.core.Controller;

/**
 * 更新编辑配置的处理组件
 * 
 * @author guxingke
 *
 */
public class PageDeleteHandleWidget extends Widget implements WidgetHandle {

	@Override
	public void handle(Controller controller) throws Typecho4jException {

		String[] cids = controller.getParaValues("cid[]");
		Long cid = null;
		for (String string : cids) {
			cid = Long.parseLong(string);
			Content.dao.deleteById(cid);
		}

		LayoutWidget.refreshLayout(IndexLayoutWidget.class);
		LayoutWidget.refreshLayout(ArchiveLayoutWidget.class);
	}

	@Override
	public void configRoute(Routes routes) {
		routes.addRoute(new Route("deletePages", this.getClass(),
				"/admin/manage-pages",
				DispatcherStrategy.REDIRECT));
	}
}
